#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/3/15 11:57
# @Author  : Wang Yuhang
# @File    : manage.py
# @Func    : 用于启动程序

from app import create_app, db
from app.models import User, Role, Post
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand

import pymysql
pymysql.install_as_MySQLdb()

import os
from dotenv import load_dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path)

app = create_app('default')  # 设定开发环境
manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role, Post=Post)


@manager.command
def test():
    import unittest
    tests = unittest.TestLoader().discover('tests')  # tests包
    unittest.TextTestRunner(verbosity=2).run(tests)


manager.add_command('db', MigrateCommand)
manager.add_command("shell", Shell(make_context=make_shell_context))

if __name__ == '__main__':
    manager.run()



